#include "automation.h"

Automation::Automation(){}
Character Automation::TestChar(const QVector<Character>& list, char var) {
    auto it = std::find(list.begin(),list.end(),var);
    if(it == list.end()) {
        return Character(_not_match,var);
    }
    return *it;
}
TokenList Automation::Tell(const QString& str, int& ptr, int& lineCount) {
    TokenList list;
    if(ptr >= str.size()) {
        list.push_back(Token(lineCount,Word(_unknown,0)));
        return list;
    }

    if(str[ptr] == ' ') {
        list.push_back(Token(lineCount,Word(_blank,str[ptr++])));
        return list;
    }
    if(str[ptr] == '\n' || str[ptr] == '\r') {
        if(str[ptr] == '\n') {
            ++lineCount;
        }
        list.push_back(Token(lineCount,Word(_unknown,str[ptr++])));
        return list;
    }

    if(str[ptr].isDigit()) {
        QString temp;
        temp.push_back(str[ptr++]);
        while(ptr < str.size() && str[ptr].isDigit()) {
            temp.push_back(str[ptr++]);
        }
        list.push_back(Token(lineCount,Word(_unsigned_int,temp)));
        return list;
    }
    if(str[ptr].isLetter()) {
        QString temp;
        temp.push_back(str[ptr++]);
        while(ptr < str.size() && str[ptr].isLetterOrNumber()) {
            temp.push_back(str[ptr++]);
        }
        list.push_back(Token(lineCount,Word(_identifier,temp)));
        return list;
    }

    if(str[ptr] == '.') {
        if(ptr < str.size() - 1 && str[ptr + 1] == '.') {
            ptr += 2;
            list.push_back(Token(lineCount,Word(_array_delimiter,str.sliced(ptr - 2,2))));
            return list;
        }
        list.push_back(Token(lineCount,Word(_delimiter_single_char,str[ptr++])));
        return list;
    }
    if(str[ptr] == '\'') {
        list.push_back(Token(lineCount,Word(_string_delimiter,str[ptr++])));
        QString temp = "";
        while(ptr < str.size() && str[ptr] != '\'') {
            temp.push_back(str[ptr++]);
        }
        list.push_back(Token(lineCount,Word(_unknown,temp)));
        if(str[ptr] == '\'') {
            list.push_back(Token(lineCount,Word(_comment_end,str[ptr++])));
        }
        return list;
    }
    if(str[ptr] == '}') {
        // 无法匹配的注释尾符
        list.push_back(Token(lineCount,Word(_unknown,str[ptr++])));
        return list;
    }
    if(str[ptr] == '{') {
        list.push_back(Token(lineCount,Word(_comment_begin,str[ptr++])));
        QString temp = "";
        while(ptr < str.size() && str[ptr] != '}') {
            temp.push_back(str[ptr++]);
        }
        list.push_back(Token(lineCount,Word(_unknown,temp)));
        if(str[ptr] == '}') {
            list.push_back(Token(lineCount,Word(_comment_end,str[ptr++])));
        }
        return list;
    }
    if(str[ptr] == ':') {
        if(ptr < str.size() - 1 && str[ptr + 1] == '=') {
            ptr += 2;
            list.push_back(Token(lineCount,Word(_delimiter_double_char,str.sliced(ptr - 2,2))));
            return list;
        }
        list.push_back(Token(lineCount,Word(_delimiter_single_char,str[ptr++])));
        return list;
    }
    list.push_back(Token(lineCount,Word(_delimiter_single_char,str[ptr++])));
    return list;
}

Token Automation::Lookup(const QVector<Word>& keyword, Token tok) {
    auto it = std::find(keyword.begin(),keyword.end(),tok.second.GetContent());
    if(it == keyword.end()) {
        return tok;
    }
    return Token(tok.first,Word(_keyword,tok.second.GetContent()));
}
